<template>
	<view class="goods-item">
		<!-- 商品的左侧图片 -->
		<view class="goods-item-left">
			<!-- 在 radioChangeHandler 事件处理函数中，通过事件对象 e，得到商品的 goods_id 和 goods_state -->
			<!-- 商品勾选状态 -->
			<radio class="radio" :checked="item.goods_state" color="#C00000" v-if="!isRadio" @click="onChangeRadio"></radio>
			<!-- 商品图片 -->
			<image :src="item.goods_small_logo || defaultPic"></image>
		</view>
		<!-- 商品的右侧内容 -->
		<view class="goods-item-right">
			<!-- 内容标题 -->
			<view class="goods-item-title">{{ item.goods_name }}</view>
			<view class="goods-item-buttom">
				<!-- 内容价格 -->
				<view class="goods-item-price">￥{{ item.goods_price }}</view>
				<!-- 商品数量 -->
				<uni-number-box @change="changeValue" :value="item.goods_count" :min="1" v-if="!isRadio"></uni-number-box>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "my-goods",
		// 父 → 子的数据
		props: {
			item: {
				type: Object,
				defaul: {},
			},
			isRadio: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				// 默认的空图片(本人已在goods_list删除了全部无图片的商品)
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
			};
		},
		methods: {
			// 修改商品的勾选状态 子 → 父
			onChangeRadio() {
				this.$emit('radio-change', {
					// 商品的 Id
					goods_id: this.item.goods_id,
					// 商品最新的勾选状态
					goods_state: !this.item.goods_state
				})
			},

			// 数字变化触发 子 → 父
			changeValue(value) {
				this.$emit('num-change', {
					// 商品id
					goods_id: this.item.goods_id,
					// 商品数量
					goods_count: value
				})
			}
		}
	}
</script>

<style lang="scss">
	.radio {
		padding-left: 2px;
	}

	.goods-list {
		padding: 10px 5px;

	}

	.goods-item {
		display: flex;
		border-bottom: 1px solid #f0f0f0;
		background-color: #fff;

		.goods-item-left {
			margin-right: 8px;
			display: flex;
			align-items: center;
			justify-content: space-between;

			radio {
				margin: 0 2px;
			}

			image {
				width: 100px;
				height: 100px;
			}
		}

		.goods-item-right {
			padding-top: 5px;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-item-title {
				font-size: 14px;
			}

			.goods-item-buttom {
				display: flex;
				justify-content: space-between;

				.goods-item-price {
					margin-bottom: 8px;
					font-size: 16px;
					color: #c00000;
				}

				uni-number-box {
					margin-right: 10px;
				}
			}
		}
	}
</style>